import { reactive } from 'vue'
import { queryInfo } from './queryInfo'
// 证件类型自定义校验规则
const validateCertificates = (_rule: any, value: any, callback: any) => {
  const household = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
  const idCard = /^[1-9]\d{5}(?:18|19|20)\d{2}(?:0[1-9]|10|11|12)(?:0[1-9]|[1-2]\d|30|31)\d{3}[\dXx]$/

  if (queryInfo.certificatesType === '10') {
    if (idCard.test(value)) {
      callback()
    } else {
      callback(new Error('请确认所选证件类型是否与输入证件号码一致  '))
    }
  } else if (queryInfo.certificatesType === '20') {
    if (household.test(value)) {
      callback()
    } else {
      callback(new Error('请确认所选证件类型是否与输入证件号码一致'))
    }
  } else {
    callback(new Error('请确认所选证件类型是否与输入证件号码一致'))
  }
}

// 手机号码自定义规则
const validatePhone = (_rule: any, value: any, callback: any) => {
  const phoneExp = /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[1589]))\d{8}$/
  if (phoneExp.test(value)) {
    callback()
  } else {
    // 手机号码不正确
    callback(new Error('请输入正确的手机号码！'))
  }
}

// 校验规则
export const rules = reactive({
  name: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  certificatesType: [{ required: true, message: '请选择证件类型', trigger: 'blur' }],
  certificatesNo: [
    { required: true, message: '请输入证件号码', trigger: 'blur' },
    { validator: validateCertificates, trigger: 'blur' }, //自定义验证规则
  ],
  sex: [{ required: true, message: '请选择性别', trigger: 'blur' }],
  birthdate: [{ required: true, message: '请选择生日日期', trigger: 'blur' }],
  phone: [
    { required: true, message: '请输入手机号码！', trigger: 'blur' },
    { validator: validatePhone, trigger: 'blur' }, //自定义验证规则
  ],
  isMarry: [{ required: true, message: '请选择婚姻状况', trigger: 'blur' }],
  isInsure: [{ required: true, message: '请选择自费/医保', trigger: 'blur' }],
  addressSelected: [{ required: true, message: '请选择当前地址', trigger: 'blur' }],
  address: [{ required: true, message: '请输入详细地址', trigger: 'blur' }],
})
